import Vue from 'vue'
import router from '../router/index'
import store from '../store/index'
import { Toast } from 'vant'
Vue.use(Toast)

const whitelist = ['/login']
router.beforeEach((to, from, next) => {
    const token = store.state.token
    if (token) {
        if (whitelist.includes(to.path)) {
            next()
        } else {
            next()
        }
    } else {
        if (whitelist.includes(to.path)) {
            next()
        } else {
            Toast.fail('请登录')
            next('/login?from=' + to.path)
        }
    }
    next()
})


// router.beforeEach((to, from, next) => {
//   const isLogin = window.localStorage.getItem('token')
//   if (isLogin) {
//     // 如果用户信息存在则往下执行。
//     next()
//   } else {
//     // 如果用户token不存在则跳转到login页面
//     if (to.path === '/login') {
//       next()
//     } else {
//       Toast.fail('请登录')
//       next('/login')
//     }
//   }
// })